
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

/**
 * A simple class to display image strip as animation.
 *
 * @author DuyNH6B
 * @since 31/3/2013
 *
 */
public class Animation {

    // The image representing the collection of images used for animation
    Image spriteStrip;
    // The scale used to display the sprite strip
    float scale;
    // The time since we last updated the frame
    long elapsedTime;
    // The time we display a frame until the next one
    int frameTime;
    // The number of frames that the animation contains
    int frameCount;
    // The index of the current frame we are displaying
    int currentFrame;
    // Width of a given frame
    public int FrameWidth;
    // Height of a given frame
    public int FrameHeight;
    // The state of the Animation
    public boolean Active;
    // Determines if the animation will keep playing or deactivate after one run
    public boolean Looping;
    // Width of a given frame
    public Vector2 Position;

    /**
     * Called by the browser or applet viewer to inform this applet that
     * Animation has been loaded into the system. It should be called in the
     * <code>init</code> method of the Applet subclass
     *
     * @see Animation#update(long)
     * @see Animation#draw(java.awt.Graphics)
     *
     * @param texture The Image Object of the animation
     * @param position The postition to be drawn
     * @param frameWidth The width of frame to be drawn
     * @param frameHeight The height of frame to be drawn
     * @param frameCount The number of parts of the texture
     * @param frametime The time of a frame to be drawn
     * @param color The background of frame
     * @param scale The float number used when resize
     * @param looping The boolean value used the determine if the Image to be
     * redrawn after drawn for the first time
     */
    public void init(
            Image texture, Vector2 position,
            int frameWidth, int frameHeight,
            int frameCount, int frametime, float scale, boolean looping) {
        // Keep a local copy of the values passed in
        this.FrameWidth = frameWidth;
        this.FrameHeight = frameHeight;
        this.frameCount = frameCount;
        this.frameTime = frametime;
        this.scale = scale;

        Position = position;
        spriteStrip = texture;
        // Set the time to zero
        elapsedTime = 0;
        currentFrame = 0;

        // Set the Animation to active by default
        Active = true;
    }

    public void update(long gameTime) {
        if (Active == false) {
            return;
        }
        if (gameTime > elapsedTime + frameTime) {
            elapsedTime = gameTime;
            currentFrame++;
            if (currentFrame == frameCount) {
                currentFrame = 0;
                if (Looping == false) {
                    Active = false;
                }
            }
        }
    }

    /**
     * Call by Applet subclass to draw Animation
     *
     * @see Animation#init(java.awt.Image, Vector2, int, int, int, int,
     * java.awt.Color, float, boolean)
     * @see Animation#update(long)
     *
     * @param g Input Graphics so Animation can draw Image by itself (reduce
     * code in applet class)
     */
    public void draw(Graphics g) {
        if (Active) {
            g.drawImage(spriteStrip,
                    Position.x, Position.y, Position.x + FrameWidth, Position.y + FrameHeight,
                    FrameWidth * (currentFrame), 0, FrameWidth * (currentFrame + 1), FrameHeight,
                    null);
        }
    }
}